﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Web;
using ZHDJ.Data;
using ZHDJ.Data.Models.JoinParty;

namespace ZHDJ.JoinParty.BLL
{
    public class ProcesUtilHelp
    {
        public static DataSet GetProcessSet(string KID)
        {
            string sql = "select * from tblPartyJoinProcess where 1=1 and id='" + KID + "'";
            return MySqlHelper.ExecuteDataSet(sql);
        }

        public static bool InsertPartyJoinLinkExample(PartyJoinLinkExample model, string PreStepID = "")
        {
            string sql = "";
            string sql1 = "insert into tblPartyJoinLinkExample(StandID";
            string sql2 = "";
            string fiter1 = "";
            string fitre2 = "";
            if (!string.IsNullOrEmpty(PreStepID))
            {
                fiter1 = "PreStepID";
                fitre2 = ",'{11}'";
            }
            string.Format(@"insert into tblPartyJoinLinkExample(StandID,CurrentStepID,CurrentStepName,Status,IsEffective,AppllicationID,ToDoHandleUserID,ToDoHandleUserName,HandleTime,OrderNo,CreateTime " + fiter1 +
            @") values('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}'" + fitre2 + ")"
, model.StandID, model.CurrentStepID, model.CurrentStepName, model.Status, model.IsEffective, model.AppllicationID, model.ToDoHandleUserID, model.ToDoHandleUserName, model.HandleTime, model.OrderNo, model.CreateTime, PreStepID);
            return MySqlHelper.ExecuteNonQuery(sql) > 0;
        }

        public static bool UpdatePartyJoinLinkExample(PartyJoinLinkExample model)
        {
            bool flag = true;

            flag = ZHDJ.WebPage.ControlBusiness.DB.SaveChanges() > 0;
            return flag;
        }

        /// <summary>
        /// 获取流程信息
        /// </summary>
        /// <param name="StandID">流程ID</param>
        /// <returns></returns>
        public static DataSet GetLinkExampleData(string StandID)
        {
            string sql = "select * from tblPartyJoinLinkExample where StandID='" + StandID + "'";
            return MySqlHelper.ExecuteDataSet(sql);
        }

        /// <summary>
        /// 取消入党||完成入党流程（最后一步）
        /// </summary>
        public static bool CancelOrCompleteJoin(string AppllicationID, int statues, bool IsLast = false)
        {
            bool flag = true;
            if (!IsLast)
            {
                DataSet ds = GetPartyJoinApplycation(AppllicationID);
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //更新用户信息
                    string sql = " update tblpartyorgmember set runState=-1,OrderNo=0,IsAgree=0,PoliticsLineament='',BeRegularDate='9999-12-31',JoinPartyDate='9999-12-31' where LoginID='" + ds.Tables[0].Rows[0]["LoginID"].ToString() + "'";
                    flag = MySqlHelper.ExecuteNonQuery(sql) > 0;
                }
                else
                    flag = false;
            }
            //修改申请单状态
            if (flag || IsLast)
            {
                string sql2 = "update tblPartyJoinApplycation set Status=" + statues + " where AppllicationID='" + AppllicationID + "'";
                flag = MySqlHelper.ExecuteNonQuery(sql2) > 0;
            }
            return flag;
        }

        /// <summary>
        /// 获取下一步流程信息
        /// </summary>
        /// <param name="CurrentSteID"></param>
        /// <returns></returns>
        public static PartyJoinProcess GetNextProcess(string CurrentSteID)
        {
            DataSet ds = new DataSet();
            string sql = @"select * from tblPartyJoinProcess 
                where StepNo>(select StepNo from tblPartyJoinProcess where ID='" + CurrentSteID + "') ORDER BY StepNo LIMIT 0,1";
            ds = MySqlHelper.ExecuteDataSet(sql);
            if (ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            return ToProcessSet(ds.Tables[0].Rows[0]);
        }

        /// <summary>
        /// 获取自定义退回流程信息
        /// </summary>
        /// <param name="ID">步骤ID</param>
        /// <returns></returns>
        public static PartyJoinProcess GetCustomBackProcess(string ID)
        {
            string sql = "select * from tblPartyJoinProcess where ID='" + ID + "'";
            DataSet ds = new DataSet();
            ds = MySqlHelper.ExecuteDataSet(sql);
            if (ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            return ToProcessSet(ds.Tables[0].Rows[0]);
        }

        /// <summary>
        /// 获取退回
        /// </summary>
        /// <param name="ID">步骤ID</param>
        /// <returns></returns>
        public static PartyJoinProcess GetBackProcess(string ID)
        {
            string sql = "";
            //获取当前步骤退回信息
            PartyJoinProcess processSet = GetCustomBackProcess(ID);
            if (!string.IsNullOrEmpty(processSet.BackToStepID))
            {
                sql = "select * from tblPartyJoinProcess where ID = (select BackToStepID from tblPartyJoinProcess where ID='" + ID + "')";
            }
            else
            {
                sql = @"select * from tblPartyJoinProcess 
                        where StepNo = 
                        (select MAX(StepNo) from tblPartyJoinProcess where StepNo < (select StepNo from tblPartyJoinProcess where ID='" + ID + "' ORDER BY StepNo LIMIT 0,1))";
            }

            DataSet ds = new DataSet();
            ds = MySqlHelper.ExecuteDataSet(sql);
            if (ds.Tables[0].Rows.Count < 1)
            {
                return null;
            }
            return ToProcessSet(ds.Tables[0].Rows[0]);
        }

        /// <summary>
        /// 更新申请表状态
        /// </summary>
        /// <param name="AppllicationID"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static bool UpdatePartyJoinApplycation(string AppllicationID, int status)
        {
            string sql = "update tblPartyJoinApplycation set status='" + status + "' where ID='" + AppllicationID + "'";
            return MySqlHelper.ExecuteNonQuery(sql) > 0;
        }
        private static PartyJoinProcess ToProcessSet(DataRow dr)
        {
            PartyJoinProcess set = new PartyJoinProcess();
            set.BackToStepID = Convert.ToString(dr["BackToStepID"]);
            set.Description = Convert.ToString(dr["Description"]);
            set.ID = Convert.ToString(dr["ID"]);
            set.ProcessName = Convert.ToString(dr["ProcessName"]);
            set.StepNo = Convert.ToInt32(dr["StepNo"]);
            set.WorkPoint = Convert.ToString(dr["WorkPoint"]);
            set.IsSendMassage = Convert.ToBoolean(dr["IsSendMassage"]);
            return set;
        }

        /// <summary>
        /// 获取申请表信息
        /// </summary>
        /// <param name="ApplycationID"></param>
        /// <returns></returns>
        public static DataSet GetPartyJoinApplycation(string ApplycationID)
        {
            string sql = "select * from tblPartyJoinApplycation where ID='" + ApplycationID + "'";
            return MySqlHelper.ExecuteDataSet(sql);
        }

        /// <summary>
        /// 获取申请
        /// </summary>
        /// <param name="appllicationID"></param>
        /// <returns></returns>
        public static int GetMaxOrderNo(string appllicationID)
        {
            string sql = "SELECT IFNULL(max(OrderNo),0) FROM tblPartyJoinLinkExample where AppllicationID='" + appllicationID + "'";
            object o = MySqlHelper.ExecuteScalar(sql);
            if (o != null)
                return Convert.ToInt32(o);
            else return 0;
        }
    }
}